import { ref, computed, watch } from 'vue'
import { useRoute } from 'vue-router'
import MenuConfig from '@/router/menu'
export default function() {

  const route = useRoute()
  const currentPath = ref('/')
  const menus = computed(() => {
    const arr = []
    for (const c of MenuConfig) {
      const { name, tid, route, hide } = c
      if (hide) {
        continue
      }
      arr.push({
        title: name,
        path: tid === 0 ? route : `/channel/${tid}`
      })
    }
    return arr
  })

  const matchPath = (arr, id) => {
    return arr.findIndex(c => c.tid === id)
  }        
  // 设置一级菜单的选中
  const setCurrentPath = route => {            
    const { path, name, params: { id } } = route
    if (name === 'channel') {                
      const matchIndex = matchPath(MenuConfig, id - 0)            
      if (matchIndex > -1) {                  
        currentPath.value = `/channel/${id}`
      }                
    } else {
      if (path === '/') {
        currentPath.value = path
      }                
    }
  }

  watch(() => route.params, () => {
    setCurrentPath(route)
  }, {
    immediate: true
  })

  return {
    menus,
    currentPath
  }

}